//
//  MSView.m
//  OrderingMerchantsSide
//
//  Created by iOS on 2018/9/20.
//  Copyright © 2018年 mls. All rights reserved.
//

#import "MSView.h"

@interface MSView ()
<
UIGestureRecognizerDelegate
>

@property (nonatomic, strong) MBProgressHUD *hud;

@end

@implementation MSView

- (instancetype)initWithViewModel:(id)viewModel
{
    return [self initWithFrame:CGRectMake(0, 0, MS_SCREEN_WIDTH, 0)];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = UIColor.whiteColor;
        [self ms_addSubviews];
        [self ms_bindViewModel];
    }
    return self;
}


#pragma mark - hud

- (void)showHudWithMessage:(NSString *)message
                      mode:(MBProgressHUDMode)mode
                  autoHide:(BOOL)autoHide
{
    ms_dispatch_main_async_safe(^(){
        
        [self.hud hideAnimated:YES];
        self.hud = [[MBProgressHUD alloc] init];
        self.hud.label.numberOfLines = 0;
        self.hud.removeFromSuperViewOnHide = YES;
        //灰色背景
        self.hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
        self.hud.bezelView.color = MS_HEX_COLOR_AND_ALPHA(0x333333, 0.8);
        
        [self addSubview:self.hud];
        
        [self.hud mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.center.equalTo(self);
            make.left.mas_greaterThanOrEqualTo(30 * MS_ADAPTATION_RATIO);
        }];
        
        self.hud.mode = mode;
        self.hud.label.text = message;
        [self.hud showAnimated:YES];
        if (autoHide)
        {
            [self.hud hideAnimated:YES
                        afterDelay:1.5];
        }
    });
}

- (void)hideHud
{
    ms_dispatch_main_async_safe(^(){
        
        [self.hud hideAnimated:YES];
    });
}


#pragma mark - UIGestureRecognizerDelegate

// 解决手势冲突
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    // 输出点击的view的类名
    MSLog(@"%@", NSStringFromClass([touch.view class]));
    
    // 若为UITableViewCellContentView（即点击了tableViewCell），则不截获Touch事件
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"])
    {
        return NO;
    }
    
    return  YES;
}


#pragma mark - RAC
- (void)ms_bindViewModel {}
- (void)ms_addSubviews {}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
